import time
from appium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait

desired_caps = {}
desired_caps['platformName'] = 'Android'
# # letv
# desired_caps['platformVersion'] = '6.0'
# desired_caps['deviceName'] = 'FUBI6SDQHYPFZLI7'

# 360
desired_caps['platformVersion'] = '5.1'
desired_caps['deviceName'] = '192.168.215.101:5555'
# 包名  com.chebao/.MainActivity
# 登录页面  com.chebao/.ui.activity.login2register.LoginActivity
desired_caps['appPackage'] = 'com.chebao'
# .ui.activity.WebNoTitileActivity  com.chebao/.ui.activity.WebNoTitileActivity
desired_caps['appActivity'] = '.ui.activity.login2register.LoginActivity'

desired_caps['unicodeKeyboard'] = True
desired_caps['resetKeyboard'] = True

# 获取toast
desired_caps['automationName'] = 'Uiautomator2'

driver = webdriver.Remote('http://127.0.0.1:4723/wd/hub', desired_caps)



# 输入手机号码
driver.find_element(By.ID, "com.chebao:id/login_phone").send_keys("18807552421")
# 输入密码
driver.find_element(By.ID, "com.chebao:id/psw_phone").send_keys("123456")
# 点击登录
driver.find_element(By.ID, "com.chebao:id/login_go").click()

time.sleep(2)

message = '登录'
# 验证获取toast、
def find_toast(driver, message, timeout=3):
    message = "//*[contains(@text,'" + message + "')]"
    element = WebDriverWait(driver, timeout, 0.1).until(lambda x: x.find_element(By.XPATH, message))
    return element.text
toast = find_toast(driver, message)
print(toast)












# contexts = driver.contexts
# for i in contexts:
#     print(i)


# NATIVE_APP
# WEBVIEW_com.chebao
#
# # 进入首页--》点击发现
# driver.find_element(By.ID, "com.chebao:id/re_find").click()

# # 点击已结束  By.ID, "com.chebao:id/xinshou_team"
# driver.find_element(By.ID, "com.chebao:id/xinshou_team").click()

#
# # 点击我的 id  =  com.chebao:id/re_profile
# driver.find_element(By.ID, "com.chebao:id/re_profile")
#
# # 点击出借 out[2]/android.support.v7.widget.RecyclerView/android.widget.RelativeLayout
# driver.find_element(By.XPATH, "//out[2]/android.support.v7.widget.RecyclerView/android.widget.RelativeLayout")


# # 点击平台公告列表信息
# announce_list = driver.find_elements_by_xpath("//android.support.v7.widget.RecyclerView/android.widget.FrameLayout")
# print(announce_list)
# # driver.switch_to.context("WEBVIEW_com.chebao")
# for i in announce_list:
#     print(i)
#     i.click()
#     driver.switch_to.context("WEBVIEW_com.chebao")
#     driver.find_element_by_class_name("android.widget.ImageView").click()



